<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/24
 * Time: 17:02
 */

namespace app\api\model;
use app\common\lib\exception\ApiException;
use think\Model;
use think\Db;
use think\Log;

class Information extends Model
{
    /**
     * 获取未查看的消息记录
     * @param information_id int 消息id *必传
     * @param information_status int 消息状态 0未查看 1已查看 默认为0
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function noSeeInformation(){
        $data = $_POST;
        if(empty($data)) {
            throw new ApiException('数据不合法',404);
        }

        if(!isset($data['information_id'])){
            throw new ApiException('information_id is not found',404);
        }

        //消息状态 0未查看 1已查看 默认为0
        if(!isset($data['information_status'])){
            $data['information_status'] = 0;
        }

        try{
            $information = Db::name('information')
                ->where('user_id="'.$data['information_id'].'" AND status="'.$data['information_status'].'"')
                ->select();

            return show(config('code.success'),'获取消息成功！',$information);

        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }
    }

    /**
     * 获取消息接口
     * @param information_id int 消息id *必传
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function allInformation(){
        $data = $_GET;
        if(empty($data)) {
            throw new ApiException('数据不合法',404);
        }

        if(!isset($data['information_id'])){
            throw new ApiException('information_id is not found',404);
        }
        try{
            $information = Db::name('information');
            $res = $information
                ->where('user_id='.$data['information_id'])
                ->order('id desc')
                ->paginate(5);

            return show(config('code.success'),'获取消息成功！',$res);

        }catch (\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),400);
        }
    }

    /**
     * 查看所有消息接口
     * @param information_id int 消息id *必传
     * @param information_status int 消息状态 0未查看 1已查看
     * @author 王柯 <wangke.email@foxmail.com>
     * @return array|string|\think\response\Json
     */
    public function seeAllInformation(){
        $data = $_GET;

        if(empty($data)) {
            throw new ApiException('数据不合法',404);
        }

        if(!isset($data['information_id'])){
            throw new ApiException('information_id is not found',404);
        }

        try{
            $information = Db::name('information')
                ->where('user_id',$data['information_id'])
                ->where('status',0)
                ->select();

            if($information){

                $data['status'] = 1;
                $res = [];
                foreach($information as $k=>$v){
                    $res[] = Db::name('information')
                        ->where('id',$information[$k]['id'])
                        ->setField('status',$data['status']);
                }
                if(count($res) == count($information)){
                    return show(config('code.success'),'查看所有消息成功！');
                }else{
                    return show(config('code.error'),'查看所有消息失败!');
                }
            }else{
                return show(config('code.error'),'所有信息已查看，勿须重复操作');
            }
        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }
}